package cd.safe.school.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.os.Environment;

public class XmlUtil {
	public static String musicname;
	public static String textname;
	public static List<String> imgnames = new ArrayList<String>();

	public static void writexml(String path, String muc, String text,
			List<String> img) {
		File filexml = new File(path);

		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.newDocument();
			Element root = doc.createElement("courseware");
			doc.appendChild(root);

			Element music = doc.createElement("music");
			music.appendChild(doc.createTextNode(muc));
			root.appendChild(music);

			Element tx = doc.createElement("text");
			tx.appendChild(doc.createTextNode(text));
			root.appendChild(tx);

			Element imgs = doc.createElement("imgs");
			root.appendChild(imgs);
			for (String s : img) {
				Element im = doc.createElement("img");
				im.appendChild(doc.createTextNode(s));
				imgs.appendChild(im);

			}
			Transformer transformer = TransformerFactory.newInstance()
					.newTransformer();

			Source source = new DOMSource(doc);

			// OutputStream outputStream = context.openFileOutput("GZ.xml",
			// Context.MODE_PRIVATE);

			FileOutputStream outputStream = new FileOutputStream(filexml);

			Result result = new StreamResult(outputStream);
			transformer.transform(source, result);
			outputStream.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void writefile(String path, String text) {
		File textfile = new File(path);
		try {
			FileOutputStream out = new FileOutputStream(textfile);
			if (text != null) {
				out.write(text.getBytes());
				out.flush();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void parsexml(String xmlpath) {
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(new File(xmlpath));
			NodeList nl = doc.getElementsByTagName("music");
			if (nl != null) {
				Element ele = (Element) nl.item(0);
				musicname = ele.getTextContent();
			}
			NodeList ns = doc.getElementsByTagName("text");
			if (ns != null) {
				Element ele = (Element) ns.item(0);
				textname = ele.getTextContent();
			}
			NodeList nls = doc.getElementsByTagName("img");
			if (nls != null) {
				if (imgnames.size() == 0) {
					for (int i = 0; i < nls.getLength(); i++) {
						Element ele = (Element) nls.item(i);
						String imgname = ele.getTextContent();

						imgnames.add(imgname);
					}

				}

			}

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	public static String readfile(String path) {
		File file = new File(path);
		StringBuffer sb = new StringBuffer();
		try {
			// InputStreamReader in = new InputStreamReader(new FileInputStream(
			// file));
			// BufferedReader bur = new BufferedReader(in);
			// int a=0;
			// byte[] by=new byte[1024];
			// while ((a=bur.read(by))>-1) {
			// String str = bur.readLine();
			// sb.append(str);
			//
			// }
			// System.out.println(sb.toString());
			FileInputStream in = new FileInputStream(file);
			byte[] a = new byte[1024];
			int count = 0;
			while ((count = in.read(a)) > 0) {
				String str = new String(a, 0, count);
				sb.append(str);

			}
			String s = sb.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();

	}
}
